﻿using BLL;
using EncryptHelper;
using MODEL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Loginter.登录用户信息
{
    public partial class UserInfo : System.Web.UI.Page
    {
        RoleBLL rolebll = new RoleBLL();
        UserBLL userbll = new UserBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindRoleList();
                BindUserList();
            }
        }
        

        /// <summary>
        /// 绑定角色列表
        /// </summary>
        private void BindRoleList()
        {
            DataTable dt = rolebll.GetRoleList();
            ddl_rolelist.DataSource = dt;
            ddl_rolelist.DataTextField = "RoleName";
            ddl_rolelist.DataValueField = "RoleID";
            ddl_rolelist.DataBind();

            ddl_rolelist.Items.Insert(0, new ListItem("==请选择==", "0"));
        }

        #region 绑定用户信息
        /// <summary>
        /// 绑定用户信息
        /// </summary>
        private void BindUserList()
        {

            //用户姓名
            string username = txt_username.Text.Trim();

            //性别：
            int sex = Convert.ToInt32(rbtnlist_sex.SelectedValue);
            //角色
            string rolename = ddl_rolelist.SelectedItem.ToString();

            //电话
            string phone = txt_phone.Text.Trim();

            //添加时间start
            DateTime? startchecktime = null;
            if (!string.IsNullOrWhiteSpace(dateinput.Value.Trim()))
            {
                startchecktime = Convert.ToDateTime(dateinput.Value);
            }

            //添加时间end
            DateTime? endchecktime = null;
            if (!string.IsNullOrWhiteSpace(dateinput.Value.Trim()))
            {
                endchecktime = Convert.ToDateTime(dateinput.Value);
            }

            //修改时间start
            DateTime? startAltertime = null;
            if (!string.IsNullOrWhiteSpace(dateinput.Value.Trim()))
            {
                startAltertime = Convert.ToDateTime(dateinput.Value);
            }

            //修改时间end
            DateTime? endAltertime = null;
            if (!string.IsNullOrWhiteSpace(dateinput.Value.Trim()))
            {
                endAltertime = Convert.ToDateTime(dateinput.Value);
            }

            //账号
            string account = txt_Account.Text.Trim();

            //电子邮箱
            string E_mail = txt_Email.Text.Trim();

            //是否删除
            int delete = Convert.ToInt32(rbtnlist_delete.Text);

            //实例化UserInfoQueryModel
            UserInfoModel querymodel = new UserInfoModel()
            {
                IsDelete=delete,
                Email=E_mail,
                Account=account,
                end_AlterTime=endAltertime,
                start_AlterTime=startAltertime,
                end_CheckInTime=endchecktime,
                start_CheckInTime=startchecktime,
                Phone=phone,
                RoleName=rolename,
                Sex=sex,
                UserName=username
            };
            List<UserInfoQueryModel> list = userbll.GetUserList(querymodel);
            gred_Role.DataSource = list;
            gred_Role.DataBind();
        } 
        #endregion

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_UserRole_Click(object sender, EventArgs e)
        {
            BindUserList();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_delete_Click1(object sender, EventArgs e)
        {
            Response.Redirect("AddUserInfo.aspx");
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gred_Role_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //分页
            BindUserList();
            gred_Role.PageIndex = e.NewPageIndex;
            gred_Role.DataBind();
        }


        protected void gred_Role_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                int userid = Convert.ToInt32(e.CommandArgument);
                int result =userbll.Updelete(userid);
                if (result == 1)
                {
                    //Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('删除成功！');</script>");
                    JSHelper.Alert(this, "删除成功！");
                    BindUserList();
                }
                else if (result == -1)
                {
                    // Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('该信息已删除！')</script>");
                    JSHelper.Alert(this, "该信息已删除！");
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('删除失败！')</script>");
                    JSHelper.Alert(this, "删除失败！");
                }
            }
        }


    }
}